<?php
class CategoryController extends Zend_Controller_Action
{
    public function init ()
    {
        
        $acl = new My_Acl();
        $auth = $acl->checkAllowed($this->_request);
        
    }
    public function indexAction ()
    {
        $model_category = new Default_Model_Category();
        $category_list = $model_category->getAllData();
        $this->view->category_list = $category_list;
    }
    public function deleteAction ()
    {
        $model_category = new Default_Model_Category();
        $id = (int)($this->_getParam('id', 0));
        $checkCategory = $model_category->checkCategory($id)->toArray();
        if($checkCategory){
            $this->view->mess = 'Nhóm này vẫn còn thiết bị. Vui lòng chuyển thiết bị qua nhóm khác!!!';
        }else{
            if ($this->getRequest()->isPost()) {
                $del = $this->getRequest()->getPost('del');
                if ($del == 'Có') {
                    $categoryId = $this->getRequest()->getPost('categoryId');
                    $model_category->deleteData($categoryId);
                }
                $this->_helper->redirector('index','category');
            } else {
                $model_category = new Default_Model_Category();
                $this->view->category = $model_category->getDataByID($id);
            }
        }
    }
    public function addAction ()
    {
        $form = new Default_Form_Category();
        $form->submit->setLabel('Thêm mới!');
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $categoryCode = $form->getValue('categoryCode');
                $categoryName = $form->getValue('categoryName');
                $note = $form->getValue('note');
                
                $auth = Zend_Auth::getInstance();
                $auth->setStorage(new Zend_Auth_Storage_Session('Zend_Auth_CMDB'));
                $userInfo = $auth->getIdentity();
                $model_users = new Default_Model_Users();
                $users_check = $model_users->getUserByUsername($userInfo['username']);
            
                $model_category = new Default_Model_Category();
                $model_category->addData($categoryCode, $categoryName, $note, $users_check->userId);
                $this->_helper->redirector('index','category');
            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction()
    {
    	$form = new Default_Form_Category();
    	$form->submit->setLabel( 'Lưu' );
    	$this->view->form = $form;
    
    	if($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData)){
    			$categoryId = (int) $form->getValue( 'categoryId' );
                $categoryCode = $form->getValue('categoryCode');
    			$categoryName = $form->getValue( 'categoryName' );
    			$note = $form->getValue( 'note' );
    
    			$model_category = new Default_Model_Category();
    			$model_category->updateData($categoryId, $categoryCode, $categoryName, $note);
    			$this->_helper->redirector('index','category');
    		}else{
    			$form->populate($formData);
    		}
    	}else{
    		$id = (int)($this->_getParam('id', 0));
    		$model_category = new Default_Model_Category();
            $category_check = $model_category->getDataByID($id);
   			$form->populate($category_check->toArray());
    	}
    }
}





